package com.socool.sinofaith.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.socool.sinofaith.R;
import com.socool.sinofaith.model.InvestigatesResponse.Investigate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class InvestigateAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private List<Investigate> list = new ArrayList<Investigate>();

	public InvestigateAdapter(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Investigate getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		StatisticsViewHolder mViewHolder = null;
		if (convertView == null) {
			mViewHolder = new StatisticsViewHolder();
			convertView = inflater.inflate(R.layout.adapter_investigates, parent, false);
			mViewHolder.bindView(convertView);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (StatisticsViewHolder) convertView.getTag();
		}
		initViewHolder(mViewHolder, position);
		return convertView;
	}

	private void initViewHolder(StatisticsViewHolder mViewHolder, int position) {
		final Investigate investigate = getItem(position);
		// mViewHolder.tvAuditDate.setText("调查日期：" +
		// investigate.getAuditdate());
		// mViewHolder.tvDealerCode.setText("经销商门店：" +
		// investigate.getDealercode());
		// mViewHolder.tvLocation.setText("地点：" + investigate.getLocation());
		// mViewHolder.tvPno.setText("计划编号：" + investigate.getPno());
		mViewHolder.tvAuditDate.setText(investigate.getAuditdate());
		mViewHolder.tvDealerCode.setText(investigate.getDealercode());
		mViewHolder.tvLocation.setText(investigate.getLocation());
		mViewHolder.tvPno.setText(investigate.getPno());
	}

	public void replace(Collection<Investigate> collection) {
		list.clear();
		list.addAll(collection);
		notifyDataSetChanged();
	}

}

class StatisticsViewHolder {
	TextView tvDealerCode;
	TextView tvAuditDate;
	TextView tvLocation;
	TextView tvPno;

	public void bindView(View convertView) {
		tvDealerCode = (TextView) convertView.findViewById(R.id.tvDealerCode);
		tvAuditDate = (TextView) convertView.findViewById(R.id.tvAuditDate);
		tvLocation = (TextView) convertView.findViewById(R.id.tvLocation);
		tvPno = (TextView) convertView.findViewById(R.id.tvPno);
	}

}
